﻿using System;
using System.Linq;
using System.Collections.Generic;
using Algoritmia.Utils;

namespace Algoritmia.DataStructures.Collections
{
    public class ListCollection<T> : ICollection<T>
    {
        private readonly Lists.IList<T> _list;

        public ListCollection(IEnumerable<T> data = null, Func<IEnumerable<T>, Lists.IList<T>> createList = null)
        {
            data = data ?? Enumerable.Empty<T>();
            createList = createList ?? (items => AlgoritmiaFactory.Lists<T>.BuildDefault(items));
            _list = createList(data);
        }        

        #region ICollection<T> Members

        public void Add(T item)
        {
            _list.Add(item);
        }

        public void Clear()
        {
            _list.Clear();
        }

        public bool Contains(T item)
        {
            return _list.Contains(item);
        }

        public bool Remove(T item)
        {
            return _list.Remove(item);
        }

        #endregion

        #region ICountable members

        public int Count
        {
            get
            {
                return _list.Count;
            }
        }

        #endregion

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        #endregion

        public override string ToString()
        {
            return String.Format("{0}<{1}>[{2}]", GetType().Name, typeof(T).Name, string.Join(", ", this));
        }
    }
}
